package com.blackhorse.order.excpetion;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.blackhorse.order.entity.Product;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

public class ExceptionUtils {
    /**
     * 静态方法
     * 返回值：SentinelClientHttpResponse
     * 参数：requset , byte[] , clientRequestException. blockException
     */

    //限流熔断业务逻辑
    public static SentinelClientHttpResponse handleBlock(HttpRequest request,
                                                         byte[] body, ClientHttpRequestExecution execution,
                                                         BlockException ex) {
        System.err.println("Oops: " + ex.getClass().getCanonicalName());
        Product product = new Product();
        product.setProductName("限流熔断...");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }
    //异常熔断业务逻辑
    public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body,
                                                            ClientHttpRequestExecution execution,
                                                            BlockException ex) {
        System.err.println("fallback: " + ex.getClass().getCanonicalName());
        Product product = new Product();
        product.setProductName("异常熔断...");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }

}